﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using SelectPdf;

namespace FreeDream.Common.FileConvert
{
    public class HtmlToPdfHelper
    {
        private static HtmlToPdfHelper instance = null;

        private HtmlToPdfHelper() { }

        public static HtmlToPdfHelper Instance
        {

            get
            {
                if (instance == null)
                {
                    instance = new HtmlToPdfHelper();
                }

                return instance;
            }
        }
        /// <summary>
        /// 將 Html 轉成 PDF，並儲存成檔案,默认的路径
        /// </summary>
        /// <param name="html"></param>
        /// <param name="desc">文件名称（描写名）</param>
        /// <param name="businessType"></param>
        /// <returns></returns>
        public static string SaveToFileByHtmlDef(string html, string desc, string businessType)
        {
            #region 
            string relativePath = Path.Combine("upload", businessType);
            //string relativePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "upload", fileRelatPath);
            var outprintfilename = desc + Guid.NewGuid().ToString("N");
            var outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath, $"{outprintfilename}.pdf");
            Instance.SaveToFileByHtml(html, outputPath);
            return Path.Combine(relativePath, $"{outprintfilename}.pdf");
            #endregion
        }


        /// <summary>
        /// 將 Html 轉成 PDF，並儲存成檔案
        /// </summary>
        /// <param name="html">html</param>
        /// <param name="fileName">絕對路徑</param>
        public void SaveToFileByHtml(string html, string fileName)
        {
            var doc = SetPdfDocument(html);
            doc.Save(fileName);
        }


        /// <summary>
        /// 傳入 Url 轉成 PDF，並儲存成檔案
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="fileName">絕對路徑</param>
        /// <param name="httpCookies">Cookies</param>
        public void SaveToFileByUrl(string url, string fileName, NameValueCollection httpCookies)
        {
            var doc = SetPdfDocument(url, httpCookies);
            doc.Save(fileName);
        }


        /// <summary>
        /// 將 Html 轉成 PDF，並輸出成 byte[] 格式
        /// </summary>
        /// <param name="html">html</param>
        /// <returns></returns>
        public byte[] GetFileByteByHtml(string html)
        {
            var doc = SetPdfDocument(html);
            return doc.Save();
        }


        /// <summary>
        /// 傳入 Url 轉成 PDF，並輸出成 byte[] 格式
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="httpCookies">Cookies</param>
        /// <returns></returns>
        public byte[] GetFileByteByUrl(string url, NameValueCollection httpCookies)
        {
            var doc = SetPdfDocument(url, httpCookies);
            return doc.Save();
        }


        /// <summary>
        /// 將 Html 轉成 PDF，並輸出成 Stream 格式
        /// </summary>
        /// <param name="html">html</param>
        /// <returns></returns>
        public Stream GetFileStreamByHtml(string html)
        {
            var doc = SetPdfDocument(html);
            var pdfStream = new MemoryStream();


            doc.Save(pdfStream);
            pdfStream.Position = 0;


            return pdfStream;
        }


        /// <summary>
        /// 傳入 Url 轉成 PDF，並輸出成 Stream 格式
        /// </summary>
        /// <param name="html">html</param>
        /// <returns></returns>
        public Stream GetFileStreamByUrl(string url, NameValueCollection httpCookies)
        {
            var doc = SetPdfDocument(url, httpCookies);
            var pdfStream = new MemoryStream();


            doc.Save(pdfStream);
            pdfStream.Position = 0;


            return pdfStream;
        }

        /// <summary>
        /// A4 2479×3508
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        private PdfDocument SetPdfDocument(string html)
        {
            var converter = new HtmlToPdf();

            converter.Options.WebPageWidth = 768;//1200
            html = System.Web.HttpUtility.HtmlDecode(html);

            return converter.ConvertHtmlString(html);
        }

        /// <summary>
        /// A4 2479×3508
        /// </summary>
        /// <param name="url"></param>
        /// <param name="httpCookies"></param>
        /// <returns></returns>
        private PdfDocument SetPdfDocument(string url, NameValueCollection httpCookies)
        {
            var converter = new HtmlToPdf();
            converter.Options.WebPageWidth = 1200;

            if (httpCookies != null && httpCookies.Count != 0)
            {
                converter.Options.HttpCookies.Add(httpCookies);
            }

            return converter.ConvertUrl(url);
        }


    }
}
